class CollectionItem {
  final String id;
  final String title;
  final String content;
  final String discuss;
  final String source;
  final String categoryId;
  final String categoryName;
  final List<String> tags;
  final bool isFavorite;
  final int showType;

  CollectionItem({
    required this.id,
    required this.title,
    required this.content,
    this.discuss = '',
    this.source = '',
    this.categoryId = '',
    this.categoryName = '',
    this.tags = const [],
    this.isFavorite = false,
    this.showType = 0,
  });

  factory CollectionItem.fromJson(Map<String, dynamic> json) {
    List<String> tagsList = [];
    if (json['tags'] != null) {
      if (json['tags'] is List) {
        tagsList = List<String>.from(json['tags']);
      } else if (json['tags'] is String) {
        // 如果是字符串，尝试按逗号分割
        tagsList = (json['tags'] as String)
            .split(',')
            .map((e) => e.trim())
            .where((e) => e.isNotEmpty)
            .toList();
      }
    }

    return CollectionItem(
      id: json['id']?.toString() ?? '',
      title: json['title'] ?? '',
      content: json['content'] ?? '',
      discuss: json['discuss'] ?? '',
      source: json['source'] ?? '',
      categoryId: json['categoryId']?.toString() ?? '',
      categoryName: json['categoryName'] ?? '',
      tags: tagsList,
      isFavorite: json['isFavorite'] ?? false,
      showType: json['showType'] ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'content': content,
      'discuss': discuss,
      'source': source,
      'categoryId': categoryId,
      'categoryName': categoryName,
      'tags': tags,
      'isFavorite': isFavorite,
      'showType': showType,
    };
  }
}
